var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        radioList:[
            { value:'1',name:'公司' },
            { value:'2',name:'个人/非企业单位' }
        ],
        orderCodes:null,
        selectedValue:null,
        invoicetitle:null,
        invoicetaxid:null,
        invoiceCost:28.4,
        invoiceContent:'*现代服务*轮椅租赁服务费',
        useremail:null,
        userphone:null,
        invoicememo:null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        var that = this
        that.setData({
            orderCodes: options.checkList,
            invoiceCost: options.invoiceCost
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    invoiceTitleChange: function(e) {
        let that = this
        console.log('发票抬头---->',e.detail.value)
        let invoicetitle = e.detail.value 
        if (invoicetitle) {
            app.executeRequest(function (header) {
                wx.request({
                  url: app.BASE_URL + '/invoice/queryEnterpriseHead',
                  header,
                  data: {
                    mc:invoicetitle
                  },
                  success: (res) => {
                    console.log('res================>',res)
                    if (that.data.selectedValue==1) {
                        that.setData({
                            invoicetitle:res.data.data[0].ghdwmc,
                            invoicetaxid:res.data.data[0].ghdwdm
                        })
                    } else if (that.data.selectedValue==2) {
                        that.setData({
                            invoicetitle:invoicetitle
                        })
                    }          
                  }
                })
              })
        } else {
            wx.showToast({
                icon:'error',
                title: '请输入发票抬头',
            })
        }
    },

    invoicetaxidChange: function(e) {
        console.log('公司税号---->',e.detail.value)
        let invoicetaxid = e.detail.value 
        if (invoicetaxid) {
            this.setData({
                invoicetaxid
            })
        } else {
            wx.showToast({
                icon:'error',
                title: '请输入公司税号',
            })
        }
    },

    radioChange: function(e) {
        console.log('发票类型-------->',e.detail.value)
        let selectedValue = e.detail.value
        this.setData({
            selectedValue
        })
    },

    // 邮箱验证部分
    inputemail: function (e) {
        let useremail = e.detail.value
        if (useremail) {
            let checkedNum = this.checkEmail(useremail)   
            if (checkedNum) {
                this.setData({
                    useremail
                })
            } 
        }
        
    }, 
    checkEmail: function (useremail) {
        let str = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
        if (str.test(useremail)) {
            return true
        } else {
            wx.showToast({
                icon:'error',
                title: '请填写正确的邮箱号',
            })
            return false 
        }
    },
    inputphone: function(e) {
        let userphone = e.detail.value
        if (userphone) {
            let checkPhone = this.checkPhone(userphone)
            if (checkPhone) {
                this.setData({
                    userphone
                })
            }
            
        }
    },
    checkPhone: function (phone) {
        let str = /^[1][3-9][0-9]{9}$/
        if (str.test(phone)) {
            return true
        } else {
            wx.showToast({
                icon:'error',
                title: '请填写正确的手机号',
            })
            return false 
        }
    },
    invoiceTip: function() {
        wx.navigateTo({
          url: '../invoiceInstruction/invoiceInstruction',
        })
    },
    changeinvoicememo:function(e) {
        console.log('备注信息------->',e.detail.value)
        let invoicememo = e.detail.value
        this.setData({
            invoicememo
        })
    },
    validateForm() {
        if(this.data.selectedValue==1) {
            if(!this.data.invoicetitle) {
                wx.showToast({
                    icon:'error',
                    title: '请输入发票抬头',
                })
                return false
            }
            if(!this.data.invoicetaxid) {
                wx.showToast({
                    icon:'error',
                    title: '请输入公司税号',
                })
                return false
            }
        } else if (this.data.selectedValue==2) {
            if(!this.data.invoicetitle) {
                wx.showToast({
                    icon:'error',
                    title: '请输入发票抬头',
                })
                return false
            }
        }
        if(!this.data.useremail && !this.data.userphone) {
            wx.showToast({
                icon:'error',
                title: '请填写邮箱或者手机号',
            })
            return false
        }
        if ((this.data.useremail && !this.checkEmail(this.data.useremail)) && !this.data.userphone ) {
            wx.showToast({
                icon:'error',
                title: '请填写正确邮箱',
            })
            return false
        }
        if ((this.data.userphone && !this.checkPhone(this.data.userphone)) && !this.data.useremail ) {
            wx.showToast({
                icon:'error',
                title: '请填写正确手机号',
            })
            return false
        }
        if ((this.data.useremail && !this.checkEmail(this.data.useremail)) && (this.data.userphone && !this.checkPhone(this.data.userphone))){
            wx.showToast({
                icon:'error',
                title: '请填写正确手机号或者邮箱',
            })
            return false
        }
        return true
    },
    submitInvoice:function() {
        let that = this
        if (that.validateForm()){
            app.executeRequest(function (header) {
                wx.request({
                  url: app.BASE_URL + '/invoice/fillCommit',
                  header,
                  data: {
                    orderCodes: that.data.orderCodes,
                    useremail: that.data.useremail,
                    userphone: that.data.userphone,
                    invoicetitle: that.data.invoicetitle,
                    invoicememo: that.data.invoicememo ? that.data.invoicememo : '',
                    invoicetaxid: that.data.invoicetaxid ? that.data.invoicetaxid : ''
                  },
                  success: (res) => {
                    console.log(res)
                    if (res.data.result==0) {
                        wx.navigateTo({
                            url: '../invoiceResult/invoiceResult',
                        })
                    } else {
                        wx.showToast({
                          icon:'error',
                          title: res.data.description,
                        })
                    }
                  }
                })
          
              })
            
        }
        
    }
})